import numpy as np
from scipy.sparse import coo_matrix
from scipy.sparse import csr_matrix
from scipy.io import mmread, mmwrite, mminfo
from copy import deepcopy as cpy
from disk_process import *


class sparse_matrix:

    def __init__(self, row: list, col: list, data: list, rowlen, collen, file_name):
        """

        :param row:
        :param col:
        :param data:
        :param rowlen:
        :param collen:
        :param file_name: each mutable_sparse_matrix should have a file representing it's data on disk
        """
        row, col, data = np.array(row), np.array(col), np.array(data)
        coo = coo_matrix((data, (row, col)), shape=(rowlen, collen), dtype=float)
        self.mat = coo.tocsr()  # mat就是真正的矩阵所在
        self.shape: tuple = self.mat.get_shape()  # (row,col)

    def __add__(self, other):
        """

        :type other: SparseMatrix
        """
        res = cpy(self)
        mat = self.mat + other.mat
        res.mat = mat
        res.shape = mat.get_shape()
        return res

    def __mul__(self, other):
        """

        :type other: SparseMatrix
        """
        res = cpy(self)
        mat = self.mat * other.mat
        res.mat = mat
        res.shape = mat.get_shape()
        return res

    def show(self):  # 一般不使用，化为稠密矩阵之后进行展示
        rmat = self.mat.toarray()
        print(rmat)

# _row  = np.array([0, 3, 1, 0])
# _col  = np.array([0, 3, 1, 2])
# _data = np.array([4, 5, 7, 9])
# coo = coo_matrix((_data, (_row, _col)), shape=(4, 4), dtype=int)
# a=coo.get_shape()
# print(type(a))
